package com.chlorine.sandbox;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

/**
 * Receive text input from user and let the Activity containing this Fragment
 * get that text.
 * 
 * @author Caroline Liu
 * 
 */
public class InputFragment extends Fragment {

	/**
	 * The Activity that this Fragment is attached to. This Activity has
	 * implemented the interface OnItemPostListener.
	 */
	OnItemPostListener containerActivity;

	/**
	 * Interface that this Fragment's containing Activity must implement.
	 * Contains one method which takes a String (=string-ified text input from
	 * user) and posts it to the list.
	 * 
	 * @author Caroline Liu
	 */
	public interface OnItemPostListener {

		/**
		 * Take s and post it to the list.
		 * 
		 * @param s
		 */
		public void onItemPostListener(String s);
	}

	/**
	 * Attach this InputFragment to activity, which must have already
	 * implemented the interface OnItemPostListener.
	 */
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		// Make sure that the container activity has implemented the callback
		// interface. If not, throw an exception.
		try {
			containerActivity = (OnItemPostListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnItemPostListener");
		}
	}

	/**
	 * Display this InputFragment and set up a listener for its Button.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment.
		View v = inflater.inflate(R.layout.frg_input, container, false);

		// Set up a listener that listens for a click event on the "Add"
		// button. On click, call the postItem() method of this InputFragment
		// and pass it the "Add" Button's view.
		Button b = (Button) v.findViewById(R.id.btn_add);
		b.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				postItem(view);
			}
		});

		// Return this InputFragment's View, because that is what onCreateView
		// does!
		return v;
	}

	/**
	 * Called when the "Add" Button is clicked. Invokes this Fragment's
	 * container Activity's onItemPostListener method, which will post the
	 * string the user entered to the list.
	 * 
	 * @param v
	 */
	public void postItem(View v) {
		// How this works:
		// (1) v is the Button; from this level of the View hierarchy,
		// we can't get at the EditText!
		// (2) So we get v's parent, which is this InputFragment. We need to
		// cast it as a View.
		// (3) From here, we can see the EditText.
		EditText t = (EditText) ((View) v.getParent())
				.findViewById(R.id.input_item);

		// Convert t to a plain Java string and trim excess whitespace.
		String s = t.getText().toString().trim();

		// If the resulting string is not empty, give it to the listener.
		if (!"".equals(s)) {
			containerActivity.onItemPostListener(s);
			// Reset t to an empty string, so that the user doesn't have to
			// delete it him/herself before typing something new.
			t.setText("");
		}
	}
}